import {Component, OnInit, TemplateRef} from '@angular/core';
import {LocalDataSource} from 'ng2-smart-table';
import {NbDialogService} from '@nebular/theme';
import {FoodService} from '../../service/food.service';
import {NbToastrService} from '@nebular/theme';
import {NbToastStatus} from '@nebular/theme/components/toastr/model';



@Component({
  selector: 'app-food',
  templateUrl: './food.component.html',
  styleUrls: ['./food.component.css']
})
export class FoodComponent implements OnInit {

  settings = {
    noDataMessage: '暂无数据',
    add: {
      addButtonContent: '<i class="nb-plus"></i>',
      createButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
      confirmCreate: true,
    },
    edit: {
      editButtonContent: '<i class="nb-edit"></i>',
      saveButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
      confirmSave: true,
    },
    delete: {
      deleteButtonContent: '<i class="nb-trash"></i>',
      confirmDelete: true,
    },
    columns: {
      id: {
        title: 'ID',
        type: 'number',
        editable: false,
      },
      Name: {
        title: '食品名称',
        type: 'string',
      },
      Spec: {
        title: '规格',
        type: 'string',
      },
      Guarantee_period: {
        title: '保质期',
        type: 'string',
      },
      Barcode: {
        title: '条形码',
        type: 'string',
      },
      Fcondition: {
        title: '状态',
        editable: false,
        filter: {
          type: 'list',
          config: {
            selectText: '选择状态',
            list: [
              {value: '审核通过', title: '审核通过'},
              {value: '审核未通过', title: '审核未通过'},
            ]
          }
        }
      }
    },
  };

  source: LocalDataSource = new LocalDataSource();
  public food: any = [];

  constructor(private foodService: FoodService,
              private dialogService: NbDialogService,
              private toastrService: NbToastrService,) {
    this.loadData();
  }

  async loadData() {
    const formData = new FormData();
    formData.append('FcpnId', '1');
    this.food = await this.foodService.getAll('?s=App.foodinfo.Select', formData);
    console.log(this.food);
    this.source.load(this.food.data.code);
  }

  onDeleteConfirm(event): void {
    if (window.confirm('确定删除?')) {
      event.confirm.resolve();
      console.log(event);
    } else {
      event.confirm.reject();
    }
  }

  onCreateConfirm(event): void {
    const formData = new FormData();
    if (event.newData.Name && event.newData.Spec && event.newData.Barcode && event.newData.Guarantee_period) {
      formData.append('FcpnId', '1');
      formData.append('Name', event.newData.Name);
      formData.append('Barcode', event.newData.Barcode);
      formData.append('Spec', event.newData.Spec);
      formData.append('Guarantee_period', event.newData.Guarantee_period);
      this.foodService.post('?s=App.foodinfo.Insert', formData).subscribe((data) => {
        if (data.ret === 200) {
          this.toastrService.show('创建成功', '提示', {status: NbToastStatus.SUCCESS});
          event.confirm.resolve();
          this.loadData();
        } else {
          this.toastrService.show('创建失败', '提示', {status: NbToastStatus.DANGER});
          event.confirm.reject();
        }
      });
    }
  }

  onEditConfirm(event): void {
    const formData = new FormData();
    console.log(event);
    if (event.newData.Name && event.newData.Spec && event.newData.Barcode && event.newData.Guarantee_period) {
      formData.append('id', event.newData.id);
      formData.append('Name', event.newData.Name);
      formData.append('Barcode', event.newData.Barcode);
      formData.append('Spec', event.newData.Spec);
      formData.append('Guarantee_period', event.newData.Guarantee_period);
      this.foodService.post('?s=App.foodinfo.Update', formData).subscribe((data) => {
        if (data.ret === 200) {
          this.toastrService.show('更新成功', '提示', {status: NbToastStatus.SUCCESS});
          event.confirm.resolve();
          this.loadData();

        } else {
          this.toastrService.show('更新失败', '提示', {status: NbToastStatus.DANGER});
          event.confirm.reject();
        }
      });
    }
  }

  ngOnInit() {
  }

}
